package com.message.queue.rabbit.direct;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Created by zhangbo on 2020/7/17.
 */
@RestController
public class SendDirectMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/send/{message}")
    public Object sendDirectMessage(@PathVariable(value = "message") String message) {

        String messageId = UUID.randomUUID().toString().replaceAll("-", "");

        /*方式1*/
//        rabbitTemplate.convertAndSend("DirectExchange", "DirectRouting", message);

        /*方式2：可配置更多属性*/
        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setPriority(0);   //设置优化级
//        messageProperties.setDelay(); //设置延期时间

        messageProperties.setExpiration("10000"); //消息失效时间，单位ms
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);//持久化消息

        Message myMessage = new Message(message.getBytes(), messageProperties);
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString().replaceAll("-", ""));
        correlationData.setReturnedMessage(myMessage);
        rabbitTemplate.send("myDirectExchange", "myDirectRouting", myMessage, correlationData);
        return "ok";
    }


}
